package com.zh.tea.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.github.pagehelper.PageInfo;
import com.zh.tea.entity.User;
import com.zh.tea.service.UserService;
import com.zh.tea.service.impl.UserServiceImpl;

import net.sf.json.JSONObject;

/**
 * Servlet implementation class UserServlet
 */
@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
	UserService us = new UserServiceImpl();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html; charset=utf-8");// 此句话必须在创建out之前
		HttpSession session = request.getSession();
		PrintWriter out = response.getWriter();
		String opt = request.getParameter("opt");
		System.out.println("用户的opt"+opt);
		if("login".equals(opt)) {
			String name = request.getParameter("name");
			String pwd = request.getParameter("textfield2");
			User user = us.login(name, pwd);
			if(user!=null) {
				session.setAttribute("User",user);
				if(user.getFlag()==1) {
					out.print("<script>alert('登陆成功!');location.href='/tea/admin/userUpdate.jsp'</script>");
				}else {
					out.print("<script>alert('登陆成功!');location.href='/tea/index.jsp'</script>");
				} 	
			}else {
				out.print("<script>alert('用户名或密码错误，无账号请点击注册!');location.href='index.jsp'</script>");
			}
			
		}
		if("register".equals(opt)) {
			String name = request.getParameter("name");
			String pwd = request.getParameter("textfield2");
			String rname = request.getParameter("rname");
			String email = request.getParameter("email");
			String lx = request.getParameter("lx");
			String cno = request.getParameter("cno");
			String address = request.getParameter("address");
			String phone = request.getParameter("phone");
			User user = new User();
			user.setPassname(name);
			user.setPassword(pwd);
			user.setRealname(rname);
			user.setEmail(email);
			user.setCerttype(lx);
			user.setCertno(cno);
			user.setAddress(address);
			user.setPhone(phone);
			int a = us.addUser(user);
			if(a>0) {
			 	out.print("<script>alert('注册成功!');location.href='login1.jsp'</script>");
			}else {
				out.print("<script>alert('注册失败!');location.href='register.jsp'</script>");
			}
			
		}if("myinfoshow".equals(opt)) {
			int id=((User)session.getAttribute("User")).getId();//通过传ID查询信息值
			User user = us.showinfo(id);
			if(user!=null) {
				
				session.setAttribute("user", user);
				response.sendRedirect("myinfo.jsp");
			}
		}
		if("updatemyinfo".equals(opt)) {
			Integer id=4;
			String realname=request.getParameter("realname");
			String phone=request.getParameter("phone");
			String email=request.getParameter("email");
			String certtype=request.getParameter("certtype");
			String certno=request.getParameter("cetyno");
			String address=request.getParameter("address");
			User user=new User();
			user.setId(id);
			user.setRealname(realname);
			user.setPhone(phone);
			user.setEmail(email);
			user.setCerttype(certtype);
			user.setCertno(certno);
			user.setAddress(address);
			int a=us.updateUserInfo(user, id);
			if(a>0) {
				out.print("<script>alert('修改成功!');location.href='UserServlet?opt=myinfoshow'</script>");
			}else {
				out.print("<script>alert('修改失败!');location.href='UserServlet?opt=myinfoshow'</script>");
			}
		}else if("showAll".equals(opt)){
			int index = Integer.parseInt(request.getParameter("page")==null?"1":request.getParameter("page"));
	        String pageSize = request.getParameter("limit") == null ? "10" : request.getParameter("limit");
	        PageInfo<User> getpage=us.getUserByPage(null, index, Integer.parseInt(pageSize));
	        JSONObject jsonobject=new JSONObject();
	        jsonobject.put("code", 0);
	        jsonobject.put("msg", "");
	        jsonobject.put("count", getpage.getTotal());
	        jsonobject.put("data", getpage.getList());
		    String json=jsonobject.toString();
		    out.print(json);
		}
		//用户的删除
		if("delete".equals(opt)){
			String id=request.getParameter("id");
			System.out.println("aaaaaaaa"+id);
			int count=us.delUser(Integer.parseInt(id));
			if(count>0){
				out.print("<script>alert('删除成功');location.href='/tea/admin/ShowUser.jsp'</script>");
			}else{
				out.print("<script>alert('删除失败');location.href='/tea/admin/userDel.jsp'</script>");
			}
		}
		//用户的编辑
		if("edit".equals(opt)){
			String email=request.getParameter("email");
			String phone=request.getParameter("phone");
			String address=request.getParameter("address");
			String id=request.getParameter("id");
			User user=new User();
			user.setAddress(address);
			user.setEmail(email);
			user.setPhone(phone);
			if(!"".equals(id)){
				user.setId(Integer.parseInt(id));
			}
			int count=us.updateUser(user);
			if(count>0){
				out.print("<script>alert('修改成功');location.href='/tea/admin/ShowUser.jsp'</script>");
			}else{
				out.print("<script>alert('修改失败');location.href='/tea/admin/UpdateUser.jsp'</script>");
			}
		}
		//通过id查找用户
		if("selectOne".equals(opt)){
			String id=request.getParameter("id");
			User user=us.selectOne(Integer.parseInt(id));
			session.setAttribute("user", user);
			out.print("<script>location.href='/tea/admin/UpdateUser.jsp'</script>");
			
		}
		//实时验证
		if("selName".equals(opt)) {
			String name = request.getParameter("n");
			if("".equals(name)) {
				out.print("用户名不能为空");
			}else {
				User user = new User();
				user.setPassname(name);
				PageInfo<User> pi = us.getUserByPage(user, 0, 0);
				if(pi.getList().size()!=0) {
					out.print("用户名重复");
				}					
			}	
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
